package me.zhengjie.common.utils

import cn.hutool.core.util.ObjectUtil
import me.zhengjie.common.exception.BadRequestException
/**
 * 验证工具
 * @author Kuki Wu
 * @date 2024-11-23
 */
object ValidationUtil {
    /**
     * 验证空
     */
    fun isNull(obj: Any?, entity: String, parameter: String, value: Any) {
        if (ObjectUtil.isNull(obj)) {
            val msg = "$entity 不存在: $parameter is $value"
            throw BadRequestException(msg)
        }
    }

    /**
     * 验证是否为邮箱
     */
    fun isEmail(string: String?): Boolean {
        if (string == null) {
            return false
        }
        val regEx1 = "^([a-z0-9A-Z]+[-|.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"
        return string.matches(regEx1.toRegex())
    }
}